<!--
 * @description  :附件上传
 * @descriptionDetail:附件上传
 * @copyright    : 浙江烟草
 * @author       : dw
 * @create       : 2022-10-09 22:21:21
-->
<template>
	<view class="page">
		<!-- #ifdef H5-ONLY -->
		<theme-navbar :is-white="false" :title="navTitle" :custom-back="goBack" style="z-index: 99999">
			<view class="nav-right-icon" @tap="confirm">
				确定
			</view>
		</theme-navbar>
		<!-- #endif -->
		<!-- #ifdef H5-WECHAT || H5-EMP -->
		<fab-button :content="fContent" @fabtrigger="fabtrigger" />
		<!-- #endif -->
		<view class="content">
			<warehouse-upload :file-list="officialPicList" :uploadParam="{'fiGroupCode':groupType}" upApiUrl="fileUpload" ref="officialStoreUpload" @on-list-change="changeUpload">
			</warehouse-upload>
		</view>
	</view>
</template>

<script>
	import storeService from '@/service/store/store.service.js';
	import commonService from '@/service/common/common.service.js';
	import warehouseUpload from './warehouseUpload.vue';
	import fabButton from '@/components/uni-fab.vue';

	export default {
		components: {
			fabButton,
			warehouseUpload,
		},
		data() {
			return {
				officialPicList: [],
				groupType: this.$FILE_GROUP_TYPE.store_man,
				params: {},
				fContent: [{
					iconPath: require('@/static/sou.png'),
					selectedIconPath: require('@/static/sou.png'),
					text: '筛选',
					active: false,
				}],
			}
		},
		mounted() {
			this.setRightBtns();
			this.params = this.getRouteParam();
			this.initData();
		},
		methods: {
			setRightBtns() {
				// 设置右侧标题
				// #ifdef H5-DINGDING
				this.$deviceApi.setRight("筛选", () => {
					this.confirm();
					this.setRightBtns();
				}, true, true);
				// #endif
			},
			fabtrigger(btn) {
				if (btn.index == 0) {
					this.confirm();
				}
			},
			initData() {
				if (this.params.fileId) {
					let ar = this.params.fileId.split(',');
					ar.forEach(ip => {
						this.officialPicList.push({
							gfiId: ip,
						});
					});
				} else {
					this.officialPicList = [];
				}
			},
			confirm() {
				this.$modal.showLoading('正在保存附件...');
				let fArr = [];
				this.officialPicList.forEach(io => {
					fArr.push(io.gfiId);
				});
				storeService.saveFile({
					fileId: fArr.join(','),
					id: this.params.id,
				}).then(res => {
					this.$modal.hideLoading();
					if (res.success) {
						this.backPrev({
							paramKey: "refresh",
							param: true,
						});
					} else {
						this.$u.toast(res.msg);
					}
				}).catch(e => {
					this.$modal.hideLoading();
					console.log('保存失败');
				});
			},
			// 上传/删除图片事件
			changeUpload(list, key) {
				if (list.length > 0) {
					this.officialPicList = [...list, ];
				} else {
					this.officialPicList = [];
				}
			},
		},
	}
</script>

<style>
	.nav-right-icon {
		padding-right: 15px;
		color: #FFF;
	}
</style>
